public class SortExample {
    /**
     *
     * 排序算法类模板
     */
    private static void sort(Comparable[] a){
        int N = a.length;
        for (int i=0;i<N;i++) {
            int min = i;
            for (int j=i+1;j<N;j++) {
                if (less(a[j],a[min])) {
                    min = j;
                }
            }
            exch(a,i,min);
        }
    }

    private static boolean less(Comparable v,Comparable w){
        return v.compareTo(w) < 0;
    }

    private static void exch(Comparable[] a,int i,int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static void show(Comparable[] a){
        //在单行打印数组
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
    }

    //
    private static boolean isSorted(Comparable[] a){
        //测试数组是否有序
        for(int i=1; i<a.length; i++){
            if(less(a[i],a[i-1])){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Integer[] a = new Integer[]{1, 9, 3, 6, 5};
        sort(a);
        assert isSorted(a);
        show(a);
    }
}
